﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Services;
using SchoolProject2012.Domain;

namespace SchoolProject2012.Areas.ChargeDePlacement.Controllers
{
    public class ContactController : Controller
    {
        private IModelService model;

        public ContactController(IModelService m)
        {
            model = m;
        }
        //
        // GET: /ChargeDePlacement/Contact/
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return RedirectToAction("ATC");
        }

        #region Visualisation
        public ActionResult ATC(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return View(model.getListOfContactEntreprise());
        }
        public ActionResult ATC(string langue, int idEntreprise)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return View(model.getListOfContactEntreprise(idEntreprise));
        }
        public ActionResult AC(string langue, int id)
        {
            Contactentreprise c = model.getContactEntreprise(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return View(c);
        }
        #endregion

        #region Ajout

        public ViewResult NC(string langue)
        {
            ViewBag.entreprises = new SelectList(model.getListOfEntreprise(), "id", "libelle");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            return View();
        }

        [HttpPost]
        public ViewResult NC(string langue, Contactentreprise c)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            if (c.isValidEntreprise())
                model.addContactEntreprise(c);
            else
                return View("ER", c);
            return View("AC", c);
        }

        #endregion

        #region Modification

        public ActionResult MC(string langue, int id)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            Contactentreprise ent = model.getContactEntreprise(id);
            if (ent == null)
            {
                return HttpNotFound();
            }

            return View(ent);
        }

        [HttpPost]
        public ViewResult MC(string langue, Contactentreprise c)
        {
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            if (c.isValidEntreprise())
            {
                if (model.getContactEntreprise(c.id) == null)
                {
                    return View("ER", c);
                }
                if (ModelState.IsValid)
                {
                    model.updateContactEntreprise(c);
                }
            }
            return View("AC", c);
        }

        #endregion

        #region Suppresion

        [HttpPost]
        public ActionResult SC(int id)
        {
            Contactentreprise c = model.getContactEntreprise(id);
            if (c == null)
            {
                return View("ER");
            }
            ViewBag.result = model.deleteEntreprise(id);
            return View();
        }

        #endregion


    }
}
